package xpn.platform.modules.template.excel;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xpn.platform.common.repository.XpnRepositoryController;
import xpn.platform.modules.attachment.AttachmentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * excel 模板 Controller
 *
 * @author duwei
 */
@Slf4j
@RepositoryRestController
@RestController
@RequestMapping("template_excel")
public class ExcelController extends XpnRepositoryController<Excel, Long> {

	@Autowired
	private ExcelService excelService;

	@Autowired
	private AttachmentService attachmentService;

	@Autowired
    ExcelDao excelDao;

	@RequestMapping(value = "service/mailmerge", method = RequestMethod.GET)
	public void filterByJpql(@RequestParam(value = "templateNo") String templateNo,
			@RequestParam(value = "attachmentLink") String attachmentLink,
			@RequestParam(value = "fileName") String fileName, HttpServletRequest request,
			HttpServletResponse response) {

		String fileFullName = fileName.endsWith(".pdf") ? fileName : fileName + ".pdf";

		try {
			attachmentService.downloadAttachment(request, response, attachmentLink, "print", fileFullName, false,
					false);
		} catch (Throwable e) {
			e.printStackTrace();
			log.error("mailmerge错误：", e);
		}
	}

	@RequestMapping(value = "service/create_print_file", method = RequestMethod.POST)
	public String createPrintFile(@RequestParam(value = "templateNo") String templateNo,
			@RequestParam(value = "attachmentLink") String attachmentLink,
			@RequestParam(value = "fileName") String fileName, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String objString = IOUtils.toString(request.getInputStream(), "utf-8");
		JsonObject obj = new JsonParser().parse(objString).getAsJsonObject();
		String fileFullName = fileName.endsWith(".pdf") ? fileName : fileName + ".pdf";
		String fileExcelFullName = fileName.endsWith(".xlsx") ? fileName : fileName + ".xlsx";

		// 保存pdf文件,如果有就不保存
		excelService.mailMerge(templateNo, attachmentLink, fileFullName, obj,fileExcelFullName);
		return "createPrintFileSuccess";
	}
	
	/**
	 * excel 转换为html 打印成pdf
	 * @param templateNo
	 * @param attachmentLink
	 * @param fileName
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "service/create_print_htmlToPdf", method = RequestMethod.POST)
	public String createPrintHtmlToPdf(@RequestParam(value = "templateNo") String templateNo,
			@RequestParam(value = "attachmentLink") String attachmentLink,
			@RequestParam(value = "fileName") String fileName, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String objString = IOUtils.toString(request.getInputStream(), "utf-8");
		JsonObject obj = new JsonParser().parse(objString).getAsJsonObject();
		String fileFullName = fileName.endsWith(".pdf") ? fileName : fileName + ".pdf";

		// 保存pdf文件,如果有就不保存
		String filename=excelService.createHtmlToPdf(templateNo, attachmentLink, fileFullName, obj);
		return filename;
	}


	/**
	 * 转换为html 打印成pdf
	 * @param templateNo
	 * @param attachmentLink
	 * @param fileName
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "service/create_print_htmlToPdfNew", method = RequestMethod.POST)
	public String createPrintHtmlToPdfNew(@RequestParam(value = "templateNo") String templateNo,
			@RequestParam(value = "attachmentLink") String attachmentLink,
			@RequestParam(value = "fileName") String fileName, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String objString = IOUtils.toString(request.getInputStream(), "utf-8");
		JsonObject obj = new JsonParser().parse(objString).getAsJsonObject();
		String fileFullName = fileName.endsWith(".pdf") ? fileName : fileName + ".pdf";

		// 保存pdf文件,如果有就不保存
		String filename=excelService.createHtmlToPdfNew(templateNo, attachmentLink, fileFullName, obj);
		return filename;
	}
	
	@RequestMapping(value = "service/catalog", method = RequestMethod.GET)
	public void catalog(@RequestParam(value = "templateNo") String templateNo,
			@RequestParam(value = "datasource") String datasource,
			@RequestParam(value = "userId",defaultValue = "-1") Long userId,
			@RequestParam(value = "extractColumnKeys",defaultValue = "") String extractColumnKeys,
			@RequestParam(value = "returnFile", defaultValue = "false") boolean returnFile, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		if (returnFile) { // 直接返回结果文件给前端
			excelService.catalog(templateNo, datasource, response,userId,extractColumnKeys);
		} else { // 不返回结果文件给前端
			excelService.catalog(templateNo, datasource, null,userId,extractColumnKeys);
		}
	}

	@RequestMapping(value = "service/extract_column", method = RequestMethod.GET)
	public Object getExtractColumn(@RequestParam(value = "templateNo") String templateNo){
		return excelService.getExtractColumn(templateNo);
	}

	@RequestMapping(value = "service/get_by_no", method = RequestMethod.GET)
	public Object getByNo(@RequestParam(value = "no") String no){
		return excelDao.findByNo(no);
	}
	
	/**
	 * 打印html  //2019-08-16 zyk
	 * @param templateNo
	 * @param attachmentLink
	 * @param fileName
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "service/create_print_html", method = RequestMethod.POST)
	public String create_print_html(@RequestParam(value = "templateNo") String templateNo,
			@RequestParam(value = "attachmentLink") String attachmentLink,
			@RequestParam(value = "fileName") String fileName, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String objString = IOUtils.toString(request.getInputStream(), "utf-8");
		JsonObject obj = new JsonParser().parse(objString).getAsJsonObject();
		String fileFullName = fileName.endsWith(".html") ? fileName : fileName + ".html";

		// 保存pdf文件,如果有就不保存
		String filename=excelService.create_print_html(templateNo, attachmentLink, fileFullName, obj);
		return filename;
	}
	
}
